| No. | Answer | Remark | |
|---|---|---|---|
| 1 | d | Prints: 1, 1, 0, 1 | Both instances of class Red share a single copy of the static field b. Although field b is only incremented using the r1 reference, the change is visible in the r2 instance of class Red. |
| 2 | c d e | Compiler error at line 3. Compiler error at line 4. Compiler error at line 5. | If a class C is declared as a member of an enclosing class then C may be declared using no access modifier or any of the three access modifiers, private, protected, or public. However, if class C is not declared as a member of an enclosing class then C may be declared with no access modifier or using the access modifier "public". The other two access modifiers, private and protected, are not applicable to any class that is not a member class. The class declaration "Class Basics4" generates a compiler error because all of the letters of the reserved word "class" must be lower case. |
| 3 | e | None of the Above | If a class is declare public, then the compiler will generate an error if the class is not stored in a file that has the same name as the class plus the extension .java. In this case, none of the classes is declared with the public modifier. Therefore, all four classes may be stored in the same file. |
| 4 | e | Compiler Error | The access modifiers public, protected, and private can not be applied to variables declared inside methods. |
| 5 | a b c d e f g j k | abstract final private protected public static synchronized native strictfp | An abstract method declaration provides no method body. If one method is declared abstract then the entire class must be declared abstract and can not be instantiated. A final method can not be overridden. The access modifiers, private, protected and public, can be applied to a method. A static method is associated with a class but not a particular instance of the class. A thread can not enter a synchronized method without first acquiring a lock. The transient and volatile modifiers are not applicable to methods. A native method is implemented in platform-dependent code. |
| 6 | a d f | If the superclass method is static, then any method with the same signature in a subclass must also be static. If the superclass method is public, then the overriding method must also be public. If the superclass method is protected, then the overriding method must be protected or public. | The modifiers synchronized, native and strictfp specify implementation details that a subclass is free to change. Similarly, a subclass can override a concrete implementation of a method with an abstract declaration. A subclass method may not have weaker access than a superclass method. For example, a public method may not be overridden by a private method. However, a subclass method can provide greater access than a superclass method. For example, a protected method can be overridden by a public method. |
| 7 | b | Prints: 0,0,0.0,null | Each array contains the default value for its type. The default value of a primitive byte or a primitive long is printed as 0. The default value of a float primitive is printed as 0.0. The default value of an Object is null and is printed as null. |
| 8 | c d e f g h | C D E F G H | The following modifiers can not be applied to a top level class: private, protected, static, synchronized, transient, volatile. The synchronized modifier can not be applied to any class because it is a method modifier. The modifiers, transient and volatile, can not be applied to any class; because they are field modifiers. |
| 9 | c d e | Compiler error at line 17. Compiler error at line 18. Compiler error at line 19. | Classes A and D are not declared in the same package; therefore, class D does not have access to package access method m4. Since class D does not extend class A, class D does not have access to the protected method, m2, of class A. |
| 10 | a b | The super class must have an accessible constructor that takes no arguments. The no-argument super class constructor must not have a throws clause. | The default constructor takes no parameters and invokes the superclass constructor with no parameters. If the superclass does not have an accessible no parameter constructor then a compile time error is generated. The default construct does not have a throws clause. Consequently, a compile time error is generated if the no parameter constructor of the super class has a throws clause. |
| 11 | c f | 3 6 | The strictfp modifier can be applied to a class or method but not to a field. The synchronized modifier is a method modifier. |
| 12 | b c d e f h i | final private protected public static transient volatile | The access modifiers, private, protected and public, can be applied to a field. A final field can not have its value assigned more than once. A transient field is not part of the persistent state of an object. Transient fields are not serialized. Fields that are shared between threads may be marked volatile to force each thread to reconcile its own working copy of the field with the master copy stored in the main memory. The synchronized modifier and abstract modifier may be applied to methods but not to fields. |
| 13 | b d e f | Instance variables declared in this class or any superclass. Instance methods declared in this class or any superclass. The keyword "this". The keyword "super". | The superclass constructor invocation statement, super, is processed before the new instance of the class has been created. For that reason, the arguments to the superclass constructor invocation statement can not legally refer to members that have not been created and initialized. |
| 14 | a d | A constructor can invoke another constructor of the same class using the constructor invocation statement "this". A constructor can invoke the constructor of the direct superclass using the superclass constructor invocation statement "super". | If a constructor invocation statement appears in the body of the constructor then it must be the first statement. The same is true for a superclass constructor invocation statement. A compile-time error is generated if a constructor attempts to invoke itself either directly or indirectly. |
| 15 | a b e | A final method can not be overridden. All methods declared in a final class are implicitly final. A machine-code generator can inline the body of a final method. | All methods declared in a final class are implicitly final. It is permissible but not required that all such methods be explicitly declared final. All private methods are implicitly final. It is permissible but not required that all private methods be explicitly declared final. The body of an inline method is inserted directly into the code at the point where the method is invoked. If the method is invoked at 10 different points in the code then the body can be copied to all 10 points. Inline code runs very quickly because it removes the need to jump to and from the method. If the method is excuted repeatedly in a loop then inlining can improve performance significantly. The machine-code generator has the option to inline a final method. |
| 16 | b | 2. | An array is declared by placing brackets after the identifer or after the type name. A compile-time error occurs at the line marked 2 because the brackets appear before the identifier for array a4. |
| 17 | a | Prints: 3,4,8 | The array, a, is an array containing 3 array objects. The first index references one of the three arrays. The second index references one of the primitive int values. |
| 18 | b | Prints: A | This question demonstrates that a Java array is really an array where each element is another array. |